home *** CD-ROM | disk | FTP | other *** search
/ Aminet 30 / Aminet 30 (1999)(Schatztruhe)[!][Apr 1999].iso / Aminet / dev / mui / bcc_src.lha / Parser / InsertFile.cpp < prev    next >
C/C++ Source or Header  |  1998-03-15  |  804b  |  59 lines

  1. #include "InsertFile.h"
  2. #include "Global.h"
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. short InsertFile::Insert( FILE *fh )
  7. {
  8.  
  9.     if( !data ) Load();
  10.     
  11.     if( !data || !size ) return 0;
  12.     
  13.     if( fwrite( data, 1, size, fh ) != size ) {
  14.         printf( "IO Error\n" );
  15.         return 0;
  16.     }
  17.  
  18.     return 1;
  19.  
  20. }
  21.  
  22. void InsertFile::Free( void )
  23. {
  24.  
  25.     if( data ) {
  26.         free( data );
  27.         data = 0;
  28.     }
  29.     
  30. }
  31.  
  32. void InsertFile::Load( void )
  33. {
  34.  FILE *fh;
  35.  
  36.  if( data ) Free();
  37.  
  38.     if( !(fh = fopen( fname, "r" )) ) {
  39.         char aname[ 30 ];
  40.         strcpy( aname, Prefs.incdir );
  41.         strcpy( aname+strlen( aname ), fname );
  42.         if( !(fh = fopen( aname, "r" )) ) return;
  43.     }
  44.     
  45.     if( fseek( fh, 0, SEEK_END ) ) return;
  46.     
  47.     size = ftell( fh );
  48.     if( !size ) return;
  49.     
  50.     rewind( fh );
  51.     
  52.     if( data = malloc( size ) ) {
  53.          fread( data, 1, size, fh );
  54.      }
  55.     
  56.      fclose( fh );
  57.  
  58. }
  59.